/**
 * 
 */
package edu.uoc.tds.pac4.servidor;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import edu.uoc.tds.pac4.common.TDSLanguageUtils;

/**
 * Pantalla del Servidor
 */
public class PantallaServidor extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;  //  @jve:decl-index=0:visual-constraint="192,52"
	private JLabel jLabelStatus = null;
	private JButton jButtonStart = null;
	private JButton jButtonStop = null;
	/**
	 * Referencia a la classe servidor
	 */
	ServidorInterface servidor;
	
	/**
	 * Constructor 
	 * @param _servidor referencia a la classe servidor
	 */
	public PantallaServidor(Servidor _servidor) {
		super();
		servidor = _servidor;
		initialize();
	}

	/**
	 * Posa el missatge al label de status
	 * @param text
	 */
	public void setLabelStatus(String text)
	{
		if(jLabelStatus != null)
			jLabelStatus.setText(text);
	}
	
	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize()
	{
		this.setTitle(TDSLanguageUtils.getMessage("PantallaServidor.title"));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(getJPanelContainer());
		this.setVisible(true);
		this.pack();
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelContainer() {
		if (jPanelContainer == null) {
			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			
			jLabelStatus = new JLabel();
			jLabelStatus.setText(TDSLanguageUtils.getMessage("PantallaServidor.jLabelStatus"));
			GridBagConstraints c1 = new GridBagConstraints();
			c1.gridx = 0;
			c1.gridy = 0;
			c1.fill = GridBagConstraints.BOTH;
			c1.gridwidth = 2;
			c1.ipadx = 400;
			c1.ipady = 100;
			c1.insets = new Insets(10, 10, 10, 10);
			jPanelContainer.add(jLabelStatus, c1);
			
			GridBagConstraints c2 = new GridBagConstraints();
			c2.gridx = 0;
			c2.gridy = 1;
			//c2.fill = GridBagConstraints.HORIZONTAL;
			c2.weightx = 0.5;
			c2.insets = new Insets(10, 10, 10, 10);
			jPanelContainer.add(getJButtonStart(), c2);
			
			GridBagConstraints c3 = new GridBagConstraints();
			c3.gridx = 1;
			c3.gridy = 1;
			//c3.fill = GridBagConstraints.WEST;
			c3.weightx = 0.5;
			c3.insets = new Insets(10, 10, 10, 10);
			jPanelContainer.add(getJButtonStop(), c3);
		}
		return jPanelContainer;
	}

	/**
	 * This method initializes jButtonStart	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStart() {
		if (jButtonStart == null) {
			jButtonStart = new JButton(TDSLanguageUtils.getMessage("PantallaServidor.jButtonStart"));
			jButtonStart.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(servidor.startServer()) {
						jButtonStart.setEnabled(false);
						jButtonStop.setEnabled(true);
					}
				}
			}
			);
		}
		return jButtonStart;
	}

	/**
	 * This method initializes jButtonStop	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStop() {
		if (jButtonStop == null) {
			jButtonStop = new JButton(TDSLanguageUtils.getMessage("PantallaServidor.jButtonStop"));
			jButtonStop.setEnabled(false);
			jButtonStop.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(!servidor.stopServer()) {
						jButtonStart.setEnabled(true);
						jButtonStop.setEnabled(false);
					}
				}
			}
			);
		}
		return jButtonStop;
	}
}
